
# 
# 
# library(ggplot2)
# library(dplyr)
# library(tidyr)
# library(forcats)

install.packages("scales")
library(scales)


digpay_data <- data %>%
  filter(group %in% c("gender", "income", "employment", "urbanicity")) %>%
  filter(year == 2024) %>%
  filter(countrynewwb == "LMIC") %>%
  filter(!group2 == c("unemployed", "")) %>%
  select(countrynewwb, year, group, group2, ts_account, fin_savfor_fi_mm) %>%
  mutate(group2 = recode (group2,
                          "women" = "Women",
                          "men" = "Men",
                          "poorest 40%" = "Poorest 40%",
                          "richest 60%" = "Wealthiest 60%",
                          "out of workforce" = "Out of workforce",
                          "self-employed" = "Self-\nemployed",
                          "employed for employer" = "Wage employed",
                          "rural" = "Rural",
                          "urban" = "Urban")) %>%
  group_by(group, group2) %>%
  summarise(
    ts_account = sum(ts_account, na.rm = TRUE),
    fin_savfor_fi_mm = sum(fin_savfor_fi_mm, na.rm = TRUE),
    .groups = "drop"
  ) %>%
  mutate(
    gap1 = fin_savfor_fi_mm,
    gap2 = ts_account - fin_savfor_fi_mm,
    total = ts_account
  ) %>%
  gather(cat, value, gap1, gap2) %>%
  filter(!is.na(cat), !is.na(value), !is.na(group2)) %>%
  mutate(
    total = total * 100,
    value = value * 100,
    lab = ifelse(cat == "gap1", round(total), NA)
  ) %>%
  filter(value != 0, !is.na(total))

digpay_data$group <- factor(
  digpay_data$group,
  levels = c("gender", "income", "employment", "urbanicity")
)

ggplot(digpay_data) +
  geom_bar(
    aes(
      x = factor(group2, levels = c("Women", "Men", "Poorest 40%", "Wealthiest 60%", "Out of workforce", "Self-\nemployed", "Wage employed", "Rural", "Urban")),
      y = value,
      fill = factor(cat, levels = c("gap2", "gap1"))
    ),
    stat = "identity",
    position = "stack",
    width = 0.7,
    color = "black"
  ) +
  scale_fill_manual(
    values = c("gap1" = "#5696D0", "gap2" = "#c2C0c0"),
    breaks = c("gap1", "gap2"),
    labels = c("Saved formally",
               "Owned an account but did not save formally")
  ) +
  scale_y_continuous(
    limits = c(0, 100),
    breaks = seq(0, 100, by = 20)
  ) +
  scale_x_discrete(labels = wrap_format(10)) +
  facet_grid(
    . ~ group,
    scales = "free",
    space = "free",
    switch = "both",
    labeller = labeller(
      group = c(
        "gender" = "Gender",
        "income" = "Income",
        "employment" = "Employment",
        "urbanicity" = "Urban-rural"
      )
    )
  ) +
  theme(
    strip.placement = "outside",
    strip.text.x = element_text(size = 20, family = "Nunito Sans", colour = "black", angle = 0), 
    strip.text.y = element_text(size = 20, family = "Nunito Sans", colour = "black", angle = 0),
    panel.background = element_blank(),
    strip.background = element_blank(),
    panel.grid.major.y = element_blank(),
    panel.grid.minor.y = element_blank(),
    panel.grid.major.x = element_blank(),
    panel.grid.minor.x = element_blank(),
    panel.border = element_blank(),
    legend.title = element_blank(),
    legend.position = "bottom",
    legend.justification = 'center',
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    axis.text.x = element_text(family = "Nunito Sans", color = "black", size = 20),
    axis.text.y = element_text(family = "Nunito Sans", color = "black", size = 20),
    axis.ticks.x = element_blank(),
    axis.ticks.y = element_line(color = "black"),
    plot.subtitle = element_text(size = 20, family = "Nunito Sans"),
    plot.title = element_text(hjust = 0, size = 20, family = "Nunito Sans", face = "bold"),
    plot.title.position = "plot",
    legend.text = element_text(hjust = 0, size = 20, family = "Nunito Sans"),
    legend.text.align = 0,
    legend.key.size = unit(4, "mm"),
    plot.caption = element_text(hjust = 0, size = 16, family = "Nunito Sans"),
    plot.caption.position = "plot"
  ) +
  guides(fill = guide_legend(nrow = 2, byrow = TRUE)) +
  labs(
    caption = "Source: Global Findex Database 2025",
    subtitle = "Adults with an account (%), 2024",
    title = "Women, poor adults, those out of the workforce, and rural account owners were less likely to save formally than\nmen, wealthier, and urban account owners"
  )


ggsave(
  filename = file.path(folder_path, "3.1.3.png"),
  width = width_num+4,
  height = height_num+3,
  device = 'png',
  dpi = 120
)

# ggsave(
#   filename = file.path(folder_path, "3.3.4.eps"),
#   width = width_num+3,
#   height = height_num+3,
#   device = 'eps',
#   dpi = 120
# )
